package net.lmc.realtime.jtp.dim.sink;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.HBaseConfiguration;

public class DimHbaseSinkFunction1 extends RichSinkFunction<String> {

    private Connection connection = null ;
    @Override
    public void open(Configuration parameters) throws Exception {
        // 创建配置对象,设置属性值,比如连接ZK集群信息
        org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
        conf.set(HConstants.CLIENT_ZOOKEEPER_QUORUM, "node101,node102,node103");
        conf.set(HConstants.CLIENT_ZOOKEEPER_CLIENT_PORT, "2181");
        // 实例化Connection连接
        connection = ConnectionFactory.createConnection(conf);
    }
    @Override
    public void invoke(String value, Context context) throws Exception {
        JSONObject jsonObject = JSON.parseObject(value);
        String tableName = jsonObject.getString("hbase_table_name");
        String familyName = jsonObject.getString("family_name");
        String rowKeyColumn = jsonObject.getString("row_key_column");
        String operateType = jsonObject.getString("operate_type");
        JSONObject dataObject = jsonObject.getJSONObject("operate_data");
        // 创建表,如果表不存在时:create 't1' , 'f1'
        createHbaseTable(tableName, familyName);
        // 实例化Table对象
        Table table = connection.getTable(TableName.valueOf(tableName));
        // 插入数据:put 'dim_base_dic' , '1305' , 'info:dic_name' , '拍错'
        if ("insert".equals(operateType) || "update".equals(operateType)){
            //
            String rowKeyValue = dataObject.getString(rowKeyColumn);
            Put put = new Put(Bytes.toBytes(rowKeyValue));
            // todo  获取数据
             for (String key : dataObject.keySet()) {
                 put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(key), Bytes.toBytes(dataObject.getString(key)));
             }

             table.put(put);
        }
        //
        else if ("delete".equals(operateType)){
            //
             String rowKeyValue = dataObject.getString(rowKeyColumn);
             Delete delete = new Delete(Bytes.toBytes(rowKeyValue));
             table.delete(delete);
        }
        else {
             System.out.println("操作类型有误！");
        }
         table.close();
    }


    private void createHbaseTable(String table, String family) throws Exception {
        // a-管理对象
        Admin admin = connection.getAdmin();
        // b-表不存在时，创建表
        TableName tableName = TableName.valueOf(table);
        if(!admin.tableExists(tableName)){
            // 表描述符-构造器
            TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
            // 列簇描述符-构造器
            ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder
                    .newBuilder(Bytes.toBytes(family));
            // 表添加列簇
            tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
            // 创建表
            admin.createTable(tableDescriptorBuilder.build());
        }
        // c-关闭
        admin.close();
    }



    @Override
    public void close() throws Exception {
        super.close();
    }
}
